home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / FINDMAX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  988 b   |  32 lines

  1. /* findmax.c --- p 110 Bible */
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. int findmax(int, ...);
  5. main()
  6. {
  7.     int maxvalue;
  8.                 /* The end of the list of integers
  9.                 is marked by -9999 */
  10.     maxvalue = findmax(-1, 20, 30, 50, -9999);
  11.     printf("findmax(-1, 20, 30, 50, -9999) returns: %d\n", maxvalue);
  12.     maxvalue = findmax(1, 2, 3, 4, 5, 6, 7, 8, -9999);
  13.     printf("findmax(1, 2, 3, 4, 5, 6, 7, 8, -9999) returns: %d\n", maxvalue);
  14. }
  15.          /*-----------------------------------------------*/
  16.          /* The "findmax" finds the largest value in a list
  17.          *  of integers. It uses the "va_..." macros to get
  18.          *  the arguments. This is ANSI version. */
  19. int findmax(int firstint, ...)
  20. {
  21.     int maxval = -9999, x = 0;
  22.     va_list argp;
  23.           /* Get the first optional parameter using "va_start" */
  24.     va_start(argp, firstint);
  25.     x = firstint;
  26.     while(x != -9999)              /* -9999 marks end of arguments */
  27.     {
  28.         if(maxval < x) maxval = x;
  29.         x = va_arg(argp, int);
  30.     }
  31.     return (maxval);
  32. }